#!/usr/bin/env python
#
# Copyright 2008 Google Inc.
# Copyright 2010 Joe LaPenna
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Foursquare oAuth client."""


import oauth
import oauth_appengine


class FoursquareOAuthClient(oauth_appengine.OAuthClient):
  """Foursquare specific oauth client.

  Per: http://code.google.com/apis/gdata/articles/oauth.html
  """

  def __init__(self, oauth_consumer=None, oauth_token=None):
    super(FoursquareOAuthClient, self).__init__(
        oauth_consumer=oauth_consumer,
        oauth_token=oauth_token,
        request_token_url='http://foursquare.com/oauth/request_token',
        access_token_url='http://foursquare.com/oauth/access_token',
        authorization_url='http://foursquare.com/oauth/authorize')
    self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()


class Foursquare(object):
  """API access to Foursquare."""

  DATE_FORMAT = r'%a, %d %b %y %H:%M:%S +0000'

  def __init__(self, oauth_client):
    self.client = oauth_client

  def checkin(self, vid, shout=None, geolat=None, geolong=None):
    parameters = {
        'vid': vid,
    }
    if shout is not None:
      parameters['shout'] = shout
    if geolat is not None:
      parameters['geolat'] = geolat
    if geolong is not None:
      parameters['geolong'] = geolong

    request = oauth.OAuthRequest.from_consumer_and_token(
        self.client.get_consumer(),
        token=self.client.get_token(),
        http_method='POST',
        http_url='http://api.foursquare.com/v1/checkin.json',
        parameters=parameters)

    request.sign_request(
        self.client.signature_method,
        self.client.get_consumer(),
        self.client.get_token())

    return self.client.access_resource(request)

  def user(self):
    request = oauth.OAuthRequest.from_consumer_and_token(
        self.client.get_consumer(),
        token=self.client.get_token(),
        http_method='GET',
        http_url='http://api.foursquare.com/v1/user.json',
        parameters={})

    request.sign_request(
        self.client.signature_method,
        self.client.get_consumer(),
        self.client.get_token())

    return self.client.access_resource(request)

  def venue(self, vid):
    parameters = {'vid': vid}

    request = oauth.OAuthRequest.from_consumer_and_token(
        self.client.get_consumer(),
        token=self.client.get_token(),
        http_method='GET',
        http_url='http://api.foursquare.com/v1/venue.json',
        parameters=parameters)

    request.sign_request(
        self.client.signature_method,
        self.client.get_consumer(),
        self.client.get_token())

    return self.client.access_resource(request)

  def venues(self, geolat, geolong, limit=None, query=None):
    parameters = {
        'geolat': geolat,
        'geolong': geolong,
    }
    if limit is not None:
      parameters['l'] = limit
    if query is not None:
      parameters['q'] = query

    request = oauth.OAuthRequest.from_consumer_and_token(
        self.client.get_consumer(),
        token=self.client.get_token(),
        http_method='GET',
        http_url='http://api.foursquare.com/v1/venues.json',
        parameters=parameters)

    request.sign_request(
        self.client.signature_method,
        self.client.get_consumer(),
        self.client.get_token())

    return self.client.access_resource(request)

  def history(self, sinceid=None, limit=None):
    parameters = {}

    if sinceid is not None:
      parameters['sinceid'] = sinceid
    if limit is not None:
      parameters['l'] = limit

    request = oauth.OAuthRequest.from_consumer_and_token(
        self.client.get_consumer(),
        token=self.client.get_token(),
        http_method='GET',
        http_url='http://api.foursquare.com/v1/history.json',
        parameters=parameters)

    request.sign_request(
        self.client.signature_method,
        self.client.get_consumer(),
        self.client.get_token())

    return self.client.access_resource(request)
